using ObjFileLoader.Common;
using ObjFileLoader.Data.DataStore;
using ObjFileLoader.Data.Elements;
using ObjFileLoader.TypeParsers.Interfaces;
using System;

namespace ObjFileLoader.TypeParsers
{
	public class FaceParser : TypeParserBase, IFaceParser, ITypeParser
	{
		private readonly IFaceGroup _faceGroup;

		protected override string Keyword => "f";

		public FaceParser(IFaceGroup faceGroup)
		{
			_faceGroup = faceGroup;
		}

		public override void Parse(string line)
		{
			string[] array = line.Split(new char[1]
			{
				' '
			}, StringSplitOptions.RemoveEmptyEntries);
			Face face = new Face();
			string[] array2 = array;
			foreach (string vertexString in array2)
			{
				FaceVertex vertex = ParseFaceVertex(vertexString);
				face.AddVertex(vertex);
			}
			_faceGroup.AddFace(face);
		}

		private FaceVertex ParseFaceVertex(string vertexString)
		{
			string[] array = vertexString.Split(new char[1]
			{
				'/'
			}, StringSplitOptions.None);
			int vertexIndex = array[0].ParseInvariantInt();
			FaceVertex result = new FaceVertex(vertexIndex, 0, 0);
			if (array.Length > 1)
			{
				int num2 = result.TextureIndex = ((array[1].Length != 0) ? array[1].ParseInvariantInt() : 0);
			}
			if (array.Length > 2)
			{
				int num4 = result.NormalIndex = ((array.Length <= 2 || array[2].Length != 0) ? array[2].ParseInvariantInt() : 0);
			}
			return result;
		}
	}
}
